package com.owen.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.owen.pojo.Commodity;
import com.owen.pojo.Fenlei;
import com.owen.pojo.Fruits;
import com.owen.server.FruitsServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequestMapping(value = "/fruits")
@Controller
public class FruitsController {

    @Autowired
    private FruitsServer fruitsServer;

    @ResponseBody
    @RequestMapping(value = "/find")
    public String find(){

        Fruits fruits1 = new Fruits(1L, "黑龙江","香蕉");
        fruitsServer.addFruits(fruits1);

        PageHelper.startPage(1, 10);
        List<Fruits> fruits = fruitsServer.findAll();
        PageInfo<Fruits> pageInfo = new PageInfo<>(fruits);

        Map<String, Object> res = new HashMap<>();
        if(fruits != null){
            res.put("code", "200");
            res.put("msg", "查询成功");
            res.put("data", pageInfo);
        } else {
            res.put("code", "201");
            res.put("msg", "查询失败");
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @ResponseBody
    @RequestMapping(value = "/findAll")
    public String findAll(){
        List<Fruits> fruits = fruitsServer.findAll();

        Map<String, Object> res = new HashMap<>();
        if(fruits != null){
            res.put("code", "200");
            res.put("msg", "查询成功");
            res.put("data", fruits);
        } else {
            res.put("code", "201");
            res.put("msg", "查询失败");
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @ResponseBody
    @RequestMapping(value = "/fileExport")
    public void fileExport(Fruits fruits, HttpServletResponse response) throws ClassNotFoundException, IndexOutOfBoundsException, IOException {
        // 生成excel
        ExcelWriter writer = ExcelUtil.getWriter();
        // 数据库查询
        List<Fruits> byFruits = fruitsServer.findByFruits(fruits);
        List<Map<String, Object>> rows = byFruits.stream().map(item -> {
            Map<String, Object> maps = new HashMap<>();
            maps.put("id", item.getId().toString());
            maps.put("name", item.getName());
            maps.put("subRegion", item.getSubRegion());
            return maps;
        }).collect(Collectors.toList());

        // Title
        int columns = Class.forName("com.owen.pojo.Commodity").getDeclaredFields().length;
        writer.merge(columns - 1, "水果信息");
        // Header
        writer.addHeaderAlias("id", "ID");
        writer.addHeaderAlias("name", "商品名");
        writer.addHeaderAlias("subRegion", "分布地区");
        // Body
        writer.setColumnWidth(0, 30);
        writer.setColumnWidth(1, 30);
        writer.setColumnWidth(2, 30);
        writer.write(rows, true);
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode("水果信息-" + DateUtil.today() + ".xls", "utf-8"));
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
    }

    @ResponseBody
    @RequestMapping(value = "/findById/{id}")
    public String findView(@PathVariable("id") Long id){
        Fruits fruits = fruitsServer.findById(id);
        Map<String, Object> res = new HashMap<>();
        if(fruits != null){
            res.put("code", "200");
            res.put("msg", "查询成功");
            res.put("data", fruits);
        } else {
            res.put("code", "201");
            res.put("msg", "查询失败");
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteFruits/{id}", method = RequestMethod.DELETE)
    public String deleteFruits(@PathVariable("id") Long id){
        Integer integer = fruitsServer.deleteById(id);
        Map<String, Object> res = new HashMap<>();
        if(integer > 0){
            res.put("code", "200");
            res.put("msg", "删除成功");
        } else {
            res.put("code", "201");
            res.put("msg", "删除失败");
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @ResponseBody
    @RequestMapping(value = "/fruitsUpdate", method = RequestMethod.GET)
    public String fruitsFpdate(Fruits fruits){
        Integer integer = fruitsServer.updateByFruits(fruits);
        Map<String, Object> res = new HashMap<>();
        if(integer > 0){
            res.put("code", "200");
            res.put("msg", "修改成功");
        } else {
            res.put("code", "201");
            res.put("msg", "修改失败");
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @ResponseBody
    @RequestMapping(value = "/addFruits")
    public String addFruits(Fruits fruits){
        Integer integer = fruitsServer.addFruits(fruits);
        Map<String, Object> res = new HashMap<>();
        if(integer > 0){
            res.put("code", "200");
            res.put("msg", "修改成功");
        } else {
            res.put("code", "201");
            res.put("msg", "修改失败");
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteBatch")
    public String deleteBatch(Long [] ids){
        Integer integer = fruitsServer.deleteBatch(ids);
        Map<String, Object> res = new HashMap<>();
        if(0 == 0){
            res.put("code", "200");
            res.put("msg", "删除成功");
        } else {
            res.put("code", "201");
            res.put("msg", "删除失败");
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @ResponseBody
    @RequestMapping(value = "/findByFenlei")
    public String findByFuits(Fruits fruits, Integer xuanzhepage, String currentPage){
        // 获取页数
        Integer current = 1;
        if (currentPage != null) {
            current = Integer.valueOf(currentPage);
        }

        PageHelper.startPage(current, xuanzhepage);
        List<Fruits> fruitss = fruitsServer.findByFruits(fruits);
        PageInfo<Fruits> pageInfo = new PageInfo<>(fruitss);

        Map<String, Object> res = new HashMap<>();
        if(fruitss != null){
            res.put("code", "200");
            res.put("msg", "查询成功");
            res.put("data", pageInfo);
        } else {
            res.put("code", "201");
            res.put("msg", "查询失败");
        }
        String s = JSON.toJSONString(res);
        return s;
    }

}